<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>迭代器</title>
</head>
<body>
    <script>
        let ary = [10,20,30,40,50];
        let obj = {
            name:"张飞",
            age:20,
            gender:"男",
            [Symbol.iterator]:function(){
                let index = 0;
                let keys = Object.keys(this);
                return {
                    next:function(){
                        return {
                            value:keys[index++],
                            done:index > keys.length
                        }
                    }
                }
            }
        };

        for(let item of ary){
            console.log(item);
        }
        for(let item in obj){
            console.log(item,obj[item]);
        }
        console.log(ary[Symbol.iterator]());
        // 得到数组的迭代器对象
        // let iterator = ary[Symbol.iterator]();
        let map = new Map();
        map.set("name","关羽");
        map.set("gender","男");
        map.set("age",30);
        // 得到集合的迭代器对象
        // let iterator = map.entries();
        // 得到自定义对象的迭代器
        let iterator = obj[Symbol.iterator]();
        while(true){
            let item = iterator.next();
            if(item.done){
                break;
            }
            console.log(item.value);
        }

        
    </script>
</body>
</html>